/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.main;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.model.ICredentialProvider;
import com.aroundj2me.model.TestCredentialProvider;

public class AroundJ2ME extends MIDlet {
	private static AroundJ2ME instance = null;
	private ApplicationController applicationController = null;
	private Display display = null;
	ICredentialProvider credentialProvider = null;
	private boolean isInitialized;
	
	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
		
	}

	protected void pauseApp() {
		
	}

	protected void startApp() throws MIDletStateChangeException {
		if (!isInitialized) {
			if(this.display == null) 
				this.display = Display.getDisplay(this);
			
			if(this.instance == null)
				this.instance = this;
			
			//TODO exchange with real one
			credentialProvider = new TestCredentialProvider();
			doStartup();
			
			isInitialized = true;
		}
	}
	
	public void doStartup(){
		applicationController = new ApplicationController(this);
		applicationController.setCredentialProvider(credentialProvider);
		applicationController.loadInitialNavigationList();
	}
	
	public Display getDisplay(){
		return this.display;
	}
	
	/**
	 * returns the Singleton instance of the application MIDLet
	 * @return
	 */
	public static AroundJ2ME getInstance(){
		return instance;
	}

}
